﻿using System;

namespace KinonekoSoftware.Studio.Pages.Galleries
{
    public partial class NewInstanceViewModel : AsyncObjectDialog<InstanceCache>
    {

        private InstanceType       _type;
        private string             _customType;
        private bool               _useCustom;
        private ObjectNameSelector _selector;
        private int                _extraType;
        private bool               _hasExtraType;

        protected override bool OnReceive(DialogContext context)
        {
            if (context.Has(ContextBase.Key_Value2))
            {
                _useCustom   = true;
                _customType  = context.GetString(ContextBase.Key_Value2);
                HasExtraType = false;
            }
            else
            {
                _type = context.GetObject<InstanceType>(ContextBase.Key_Value);

                if (context.Has("t") && context.Has("s"))
                {
                    _selector = context.GetObject<ObjectNameSelector>("s");
                    ExtraTypes.AddMany(context.GetObject<IEnumerable<int>>("t"), true);
                    ExtraType    = ExtraTypes.FirstOrDefault();
                    HasExtraType = true;

                }
            }

            RaiseUpdated(nameof(TypeName));

            return base.OnReceive(context);
        }

        public ObservableCollection<int> ExtraTypes { get; }
        public bool HasExtraType
        {
            get => _hasExtraType;
            set => SetValue(ref _hasExtraType, value);
        }


        public int ExtraType
        {
            get => _extraType;
            set => SetValue(ref _extraType, value);
        }



        public string Avatar
        {
            get => _avatar;
            set => SetValue(ref _avatar, value);
        }


        public string Name
        {
            get => _name;
            set => TryFinishAndSetValue(ref _name, value);
        }

        public string TypeName => _useCustom ? _customType : LanguageManager.GetEnum(Type);

        public InstanceType Type
        {
            get => _type;
            set => SetValue(ref _type, value);
        }

        public ObjectNameSelector Selector => _selector;
    }
}